#include "X.h"

X::X(): data(0) {
	/* bloc vide */
}

X::X(const X& x) {
	lock(m, x.m);
	lock_guard<mutex> g1(m, adopt_lock);
	lock_guard<mutex> g2(x.m, adopt_lock);
	data = x.data;
}

X::X(int d): data(d) {
	/* bloc vide */
}

void swap (X&x1, X& x2) {
	lock(x1.m, x2.m);
	lock_guard<mutex> g1(x1.m, adopt_lock);
	lock_guard<mutex> g2(x1.m, adopt_lock);
	int inter = x1.data;
	x1.data = x2.data;
	x2.data = inter;
}

bool operator == (const X& x1, const X& x2) {
	if (&x1 == &x2) {
		return true;
	}
	unique_lock<mutex> ul1(x1.m, defer_lock);
	unique_lock<mutex> ul2(x2.m, defer_lock);
	lock(ul1, ul2);
	return x1.data == x2.data;
}

ostream& operator << (ostream& os, const X& x) {
	lock_guard<mutex> g(x.m);
	os << x.data;
	return os;
}

int X::getData() const {
	lock_guard<mutex> g(m);
	return data;
}

X::~X() {
	/* bloc vide */
}

